chisl = int(input("Введите четырехзначное натуральное число - "))
ed = chisl%10
des = chisl%100//10
sot = chisl%1000//100
tis = chisl//1000
sot1=chisl//10
sot2= chisl-tis*1000
des3 = chisl%100
des1 = (chisl-des3)//100
des2 = ((chisl-tis*1000)-ed)//10
ninchislo = 0

if chisl%2==0:
    ninchislo = 0
else:
    for i in range(3, int(sot1**0.5)+1, 2):
        if chisl%i == 0:
            ninchislo = 0
            break
        else:
            ninchislo=chisl
if ninchislo ==0:
    if sot1>sot2:
        if sot1%2==0:
            ninchislo = 0
        else:
            for i in range(3, int(sot1**0.5)+1, 2):
                if sot1%i == 0:
                    ninchislo = 0
                    break
                else:
                    ninchislo=sot1
        if sot2%2==0:
            ninchislo = 0
        else:
            for i in range(3, int(sot2**0.5)+1, 2):
                if sot2%i == 0:
                    ninchislo = 0
                    break
                else:
                    ninchislo=sot2
    if sot2>sot1:
        if sot2%2==0:
            ninchislo = 0
        else:
            for i in range(3, int(sot2**0.5)+1, 2):
                if sot2%i == 0:
                    ninchislo = 0
                    break
                else:
                    ninchislo=sot2
        if sot1%2==0:
            ninchislo = 0
        else:
            for i in range(3, int(sot1**0.5)+1, 2):
                if sot1%i == 0:
                    ninchislo = 0
                    break
                else:
                    ninchislo=sot1

if ninchislo==0:
    if max(des1,des2,des3)==des1:
        if des1%2==0:
            ninchislo = 0
        else:
            for i in range(3, int(des1**0.5)+1, 2): 
                if des1%i == 0:
                    ninchislo = 0
                    break
                else:
                    ninchislo=des1
if ninchislo==0:
    i=2
    if max(des1,des2,des3)==des2:
        if des2%2==0:
            ninchislo = 0
        else:
            for i in range(3, int(des2**0.5)+1, 2): 
                if des2%i == 0:
                    ninchislo = 0
                    break
                else:
                    ninchislo=des2
if ninchislo==0:
    if max(des1,des2,des3)==des3:
        if des3%2==0:
            ninchislo = 0
        else:
            for i in range(3, int(des3**0.5)+1, 2): 
                if des3%i == 0:
                    ninchislo = 0
                    break
                else:
                    ninchislo=des3
if ninchislo==0:
    if max(des1,des2,des3)==des3:
        if des3%2==0:
            ninchislo = 0
        else:
            for i in range(3, int(des3**0.5)+1, 2): 
                if des3%i == 0:
                    ninchislo = 0
                    break
                else:
                    ninchislo=des3
if ninchislo==0:
    if max(ed,des,sot,tis)==ed:
        if ed%2==0:
            ninchislo = 0
        else:
            for i in range(3, int(ed**0.5)+1, 2): 
                if ed%i == 0:
                    ninchislo = 0
                    break
                else:
                    ninchislo=ed
    elif max(ed,des,sot,tis)==des:
        if des%2==0:
            ninchislo = 0
        else:
            for i in range(3, int(des**0.5)+1, 2): 
                if des%i == 0:
                    ninchislo = 0
                    break
                else:
                    ninchislo=des
    elif max(ed,des,sot,tis)==sot:
        if sot%2==0:
            ninchislo = 0
        else:
            for i in range(3, int(sot**0.5)+1, 2): 
                if sot%i == 0:
                    ninchislo = 0
                    break
                else:
                    ninchislo=sot
    elif max(ed,des,sot,tis)==tis:
        if tis%2==0:
            ninchislo = 0
        else:
            for i in range(3, int(tis**0.5)+1, 2): 
                if tis%i == 0:
                    ninchislo = 0
                    break
                else:
                    ninchislo=tis
print(ninchislo)